iit

Basic SQL Queries


By Justin Callanan

Introduction
Database and query basics
Elements of a query
Writing an SQL query
Wrap-up
Quiz

Quiz

1. What is a SQL query used for?

A. To ask the database for help
Incorrect. A database generally does not offer help functionality like what is found in desktop operating systems.
B. To retrieve records from a database
Correct! Queries are used to extract data stored within a database.
C. To add data to a database
Incorrect. While a query is used when interacting with a database, it is not used to add data to it.
D. To change data within a database
Incorrect. While a query is used when interacting with a database, it is not used to change data within it.

2. What is needed for the most basic SQL query?

A. A SELECT statement, a WHERE clause, and a table name
Incorrect. WHERE clauses are optional, and this query would be missing several key pieces.
B. A QUERY statement, at least one column name, a FROM clause, and a table name
Incorrect. While a query must contain a column, FROM clause, and table name, there is no QUERY statement in SQL.
C. A SELECT statement, a wildcard, an IN clause, and a table name
Incorrect. While a simple query could have a wildcard in it, the SQL IN clause is not what is used to refer to a particular table.
D. A SELECT statement, at least one column name, a FROM clause, and a table name
Correct! These are the four most basic elements of an SQL query.

3. Which of the following is NOT a proper SQL query?

A. SELECT * FROM customer
Incorrect. This query contains the necessary SELECT statement, a "*" wildcard for all columns, the FROM clause, and a table name.
B. SELECT name FROM personnel
Incorrect. This query contains the necessary SELECT statement, a column name, the FROM clause, and a table name.
C. SELECT ALL FIELDS FROM tenants
Correct! If one wishes to select all fields in a table, the proper way to do it is by using the "*" wildcard.
D. SELECT * FROM receipts WHERE day="Wednesday"
Incorrect. This query contains the necessary SELECT statement, a column name, the FROM clause, and a table name, and an additional WHERE clause with a specified column and value.

4. Which of the following queries would retrieve all records in the "guests" table with the hometown "Chicago"?

A. SELECT * FROM guests IF hometown="Chicago"
Incorrect. There is no IF clause in SQL.
B. SELECT * FROM guests WHERE hometown="Chicago"
Correct! This query would return all records which had the value "Chicago" in the hometown field.
C. SELECT guests WHERE hometown="Chicago"
Incorrect. This query is missing column names (or a wildcard in place of them).
D. SELECT * WHERE hometown="Chicago" FROM guests
Incorrect. While this query has all the right pieces, the clauses are in the wrong order.

< Previous